#include <nice2d/serializer.h>

IMPLEMENT_BASE_OBJECT(Serializer)

void Serializer::saveToXml(const std::string &file)
{
    saveToXml(file,getObjectName());
}

void Serializer::saveToXml(const std::string &file,const std::string &nodeName)
{
    auto setting = boost::property_tree::xml_writer_make_settings<std::string>('\t',1);
    boost::property_tree::ptree root;
    auto node = serialize();
    root.add_child(nodeName,node);
    boost::property_tree::xml_parser::write_xml(file,root,std::locale("zh_CN.utf8"),setting);
}

template<class T>
sf::Vector2<T> deserializeVector(const PTree& node)
{
    sf::Vector2<T> value;
    value.x = node.get<T>(std::string("x"));
    value.y = node.get<T>(std::string("y"));
    return value;
}

sf::Vector2f deserializeVector2f(const PTree &node)
{
    return deserializeVector<float>(node);
}

sf::Vector2i deserializeVector2i(const PTree &node)
{
    return deserializeVector<int>(node);
}

sf::Vector2u deserializeVector2u(const PTree &node)
{
    return deserializeVector<unsigned int>(node);
}

sf::Color deserializeColor(const PTree &node)
{
    sf::Color value = sf::Color::Black;
    value.r = node.get<uint8_t>(std::string("r"));
    value.g = node.get<uint8_t>(std::string("g"));
    value.b = node.get<uint8_t>(std::string("b"));
    value.a = node.get<uint8_t>(std::string("a"));
    return value;
}

template<class T>
sf::Rect<T> deserializeRect(const PTree &node)
{
    sf::Rect<T> value;
    value.left = node.get<T>(std::string("left"));
    value.top = node.get<T>(std::string("top"));
    value.width = node.get<T>(std::string("width"));
    value.height = node.get<T>(std::string("height"));
    return value;
}

sf::IntRect deserializeIntRect(const PTree &node)
{
    return deserializeRect<int>(node);
}

sf::FloatRect deserializeFloatRect(const PTree &node)
{
    return deserializeRect<float>(node);
}

sf::VideoMode deserializeViewMode(const PTree &node)
{
    sf::VideoMode mode;
    mode.width = node.get<int32_t>(std::string("width"));
    mode.height = node.get<int32_t>(std::string("height"));
    mode.bitsPerPixel = node.get<int32_t>(std::string("bitsPerPixel"));
    return mode;
}

void deserializeObject(const PTree &node, sf::Vector2f &value)
{
    value = deserializeVector2f(node);
}

void deserializeObject(const PTree &node, sf::Vector2i &value)
{
    value = deserializeVector2i(node);
}

void deserializeObject(const PTree &node, sf::Vector2u &value)
{
    value = deserializeVector2u(node);
}

void deserializeObject(const PTree &node, sf::Color &value)
{
    value = deserializeColor(node);
}

void deserializeObject(const PTree &node, sf::IntRect &value)
{
    value = deserializeIntRect(node);
}

void deserializeObject(const PTree &node, sf::FloatRect &value)
{
    value = deserializeFloatRect(node);
}

void deserializeObject(const PTree &node, sf::VideoMode &value)
{
    value = deserializeViewMode(node);
}
